package com.wms.web.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.wms.data.pojo.SystemUser;
import com.wms.utils.HttpUtil;

/**
 * PSTS Share Action.
 * 
 * <table border="1" width="80%">
 * <tr>
 * <th>Version</th>
 * <th>Date</th>
 * <th>Role</th>
 * <th>Modified By</th>
 * <th>Details</th>
 * </tr>
 * <tr>
 * <td>%I%</td>
 * <td>2011/11/23</td>
 * <td>SD/PG</td>
 * <td>Evan</td>
 * <td>Created By</td>
 * </tr>
 * </table>
 * <p/>
 * 
 * <pre>
 *	PSTS Share Action.
 * </pre>
 *
 * @author Evan
 * @version 1.0
 * @since 1.5
 * 
 * SVN:
 * <ol>
 * 	<li>$Id$</li>
 * 	<li>$Rev$</li>
 * </ol>
 *
 */
public class AbstractAction extends ActionSupport {
	
	public static final String PROP_SESSION_CODE_MENU = "USER_MENU";
	
	public static final String CREATE_SUCCESS_MSGCODE = "PSTS.007";
	public static final String UPDATE_SUCCESS_MSGCODE = "PSTS.009";
	public static final String DELETE_SUCCESS_MSGCODE = "PSTS.011";
	
	public static final String CREATE_FAILED_MSGCODE = "PSTS.008";
	public static final String UPDATE_FAILED_MSGCODE = "PSTS.010";
	public static final String DELETE_FAILED_MSGCODE = "PSTS.012";
	
	// 2012/02/01 Luna add
	public static final String UPLOAD_READ_FAILED_MSGCODE = "PSTS.016";
	public static final String UPLOAD_OVERWRITE_MSGCODE = "PSTS.017";
	
	private static final long serialVersionUID = 1L;
	
	Logger logger = Logger.getLogger(super.getClass());
	
	// fenyebianliang
	// limit => [ 25 ] page => [ 2 ] start => [ 25 ]
	int limit;
	int page;
	int start;
	
	/**
	 * get Request Map.
	 *
	 * <pre>
	 * 	get Request Map
	 * </pre>
	 *
	 * @return String
	 */
	public Map<String, Object> getContextMap() {
		
		return ActionContext.getContext().getContextMap();
	}
	
	public HttpServletRequest getRequest() {
		
		return ServletActionContext.getRequest();
	}
	
	public HttpServletResponse getResponse() {
		
		return ServletActionContext.getResponse();
	}
	
	public String getParameter(String key) {
		
		return StringUtils.trim(getRequest().getParameter(key));
	}
	
	public String getParameter(String key, String defaultValue) {
		
		String value = getParameter(key);
		
		return StringUtils.isBlank(value) ? defaultValue : value;
	}
	
	/**
	 * get Session Map.
	 *
	 * <pre>
	 * 	get Session Map
	 * </pre>
	 *
	 * @return String
	 */
	public Map<String, Object> getSessionMap() {
		
		return ActionContext.getContext().getSession();
	}

	/**
	 * set Message Code.
	 *
	 * <pre>
	 * 	set Message Code
	 * </pre>
	 *
	 * @param messageCode
	 */
	public void setMessageCode(String messageCode) {
		
		getContextMap().put("MESSAGE_CODE", messageCode);
	}
	
	/**
	 * set Message Code.
	 *
	 * <pre>
	 * 	set Message Code
	 * </pre>
	 *
	 * @param messageCode
	 */
	public void setMessageCode(String messageCode, String status) {
		
		getContextMap().put("MESSAGE_CODE", messageCode);
		getContextMap().put("MESSAGE_STATUS", status);
	}
	
	/**
	 * Set logged on user information.
	 *
	 * <pre>
	 * 	Set logged on user information
	 * </pre>
	 *
	 * @param sysUser
	 */
	public void setLoginUser(SystemUser sysUser) {
		
		if (sysUser != null) {
			
			getSessionMap().put(SystemUser.PROP_SESSION_CODE_USER, sysUser);
			
			logger.info(">>>>>User Login>>>>>UserId:" + sysUser.getId() + " >>>>>UserName:" + sysUser.getUserName());
		}
		else {
				
				getSessionMap().remove(SystemUser.PROP_SESSION_CODE_USER);
		}
	}
	
	/**
	 * Get logged in user information.
	 *
	 * <pre>
	 * 	Get logged in user information
	 * </pre>
	 *
	 * @return String
	 */
	public SystemUser getLoginUser() {
		
		Object object = getSessionMap().get(SystemUser.PROP_SESSION_CODE_USER);
		
		if (object != null) {
			
			return (SystemUser) object;
		}
		else {
			
			return null;
		}
	}
	
	public List<Map<String, String>> getMenuSession() {
		
		Object object = getSessionMap().get(PROP_SESSION_CODE_MENU);
		
		if (object != null) {
			
			return (List<Map<String, String>>) object;
		}
		else {
			
			return null;
		}
	}
	
	public void httpWrite(int count, Object o) {
		
		try{
			
			JSONArray jsonArray = JSONArray.fromObject(o);
			
			HttpUtil.write(getResponse(), "{totalCount:" + count + ", root:" + jsonArray + "}");
		}
		catch (Throwable e) {
			e.printStackTrace();
		}
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}
}